package com.chezhujie.www.service;

import com.chezhujie.www.core.exception.ClassCheckException;
import com.chezhujie.www.core.page.Pagination;
import com.chezhujie.www.domain.Czjreply;
import com.chezhujie.www.persistence.CzjreplyMapper;
import com.chezhujie.www.persistence.CzjuserinfoMapper;
import com.chezhujie.www.utils.MapManage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 回复评论
 * Created by 汤伟杰 on 2017/5/23.
 */
@Service
public class CzjreplyService extends BaseService<Czjreply>{
    @Autowired
    private CzjreplyMapper czjreplyMapper;
    @Autowired
    private CzjuserinfoMapper czjuserinfoMapper;

    @Autowired
    public void setBaseMapper(CzjreplyMapper czjreplyMapper) {
        this.baseMapper = czjreplyMapper;
    }

    public List<Map<String,Object>> getListUserinfoId(Map<String, Object> param) {
        return czjreplyMapper.listUserinfoId(param);
    }
    public List<Map<String,Object>> getListCommentsId(Map<String, Object> param) {
        return czjreplyMapper.listCommentsId(param);
    }

    public void checkEntity(Czjreply czjreply) throws ClassCheckException {
        if(czjreply.getContent()!=null&&czjreply.getContent().length()>140){
            throw new ClassCheckException("回复评论内容不能超过140个字符");
        }
    }
    public void checkListEntity(List<Czjreply> listCzjreply) throws ClassCheckException {

        for (Czjreply czjreply : listCzjreply) {
            checkEntity(czjreply);
        }

    }
    @Override
    public void insert(Czjreply entity) {
        entity.setApproveStatus(0);
        entity.setCreatetime(new Date());
        entity.setDelFlag(0);
        super.insert(entity);
    }
    /**
     * 修改审批状态
     * @param id
     * @param approveStatus
     */
    public void changeApprove(String id, Integer approveStatus) {
        Czjreply entity = queryById(id);
        entity.setApproveStatus(approveStatus);
        entity.setApproveTime(new Date());
        update(entity);
    }
    public List<Map<String,Object>> getListByComments(List<String> listCommentsId) {
        List<Map<String, Object>> list = czjreplyMapper.getListByComments(listCommentsId);
        return list;
    }
    public Pagination apilist(int pageNo, int pageSize, Map<String, Object> params, ModelMap model) {
        Pagination pagination = new Pagination();
        pagination.setPageNo(pageNo);
        pagination.setPageSize(pageSize);
        params.putAll(pagination.toMap());
        int count = baseMapper.getApiTotalRows(params);
        List<Map<String, Object>> list = baseMapper.getApiList(params);
        pagination.setTotalCount(count);

        /** 评论用户集合 **/
        List<String> listUserinfoId = new ArrayList<String>();
        for (Map<String, Object> map : list) {
            listUserinfoId.add(String.valueOf(map.get("userinfoId")));
        }
        /** 评论用户集合查询 **/
        List<Map<String, Object>> listUserinfo = new ArrayList<Map<String, Object>>();
        if(listUserinfoId.size()!=0){
            listUserinfo = czjuserinfoMapper.getByIn(listUserinfoId);
        }
        Map<Integer, Map<String, Object>> czjuserinfoMap =    MapManage.toGetKeyMapInt("id", listUserinfo);
        for (int i = 0; i < list.size(); i++) {
            /** 补充评论用户信息 */
            if(list.get(i).get("userinfoId")!=null
                    &&czjuserinfoMap.get(Integer.valueOf(String.valueOf(list.get(i).get("userinfoId"))))!=null){
                list.get(i).putAll(czjuserinfoMap.get(Integer.valueOf(String.valueOf(list.get(i).get("userinfoId")))));
            }else{
                list.get(i).put("nickname", "");
                list.get(i).put("picPath", "");
            }

        }
        pagination.setList(list);
        return pagination;
    }
}
